package com.gky.gkytemplate.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gky.gkytemplate.annotation.AuthCheck;
import com.gky.gkytemplate.common.constant.UserConstant;
import com.gky.gkytemplate.common.enums.ErrorCode;
import com.gky.gkytemplate.common.properties.JwtProperties;
import com.gky.gkytemplate.common.result.Result;
import com.gky.gkytemplate.pojo.dto.user.*;
import com.gky.gkytemplate.pojo.entity.User;
import com.gky.gkytemplate.pojo.vo.UserLoginVo;
import com.gky.gkytemplate.service.UserService;
import com.gky.gkytemplate.utils.JwtUtils;
import com.gky.gkytemplate.utils.ThrowUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVo> login(@RequestBody UserLoginDto userLoginDto) {
        ThrowUtils.throwIf(userLoginDto == null, ErrorCode.NOT_FOUND_ERROR, "账号和密码为空");
        UserLoginVo userLoginVo = userService.login(userLoginDto);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userLoginVo.getId());
        String token = JwtUtils.createJWT(jwtProperties.getSecretKey(), jwtProperties.getTtl(), map);
        userLoginVo.setToken(token);
        redisTemplate.opsForValue().set(UserConstant.USER_LOGIN + "_" + userLoginVo.getId(), userLoginVo);
        return Result.ok(userLoginVo);
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<Boolean> logout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = userService.userLogout(request);
        return Result.ok(result);
    }

    /**
     * 逻辑删除用户  权限校验
     * @param userDeleteRequest
     * @return
     */
    @DeleteMapping("/deleteUser")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result<Boolean> deleteUser(@RequestBody UserDeleteRequest userDeleteRequest) {
        ThrowUtils.throwIf(userDeleteRequest == null || userDeleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = userService.removeById(userDeleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return Result.ok(true);
    }

    /**
     * 更新用户
     * @param userUpdateRequest
     * @return
     */
    @PostMapping("/updateUser")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        ThrowUtils.throwIf(userUpdateRequest == null, ErrorCode.PARAMS_ERROR);
        User user = new User();
        BeanUtils.copyProperties(userUpdateRequest, user);
        boolean result = userService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return Result.ok(true);
    }

    /**
     * 注册
     * @param userAddRequest
     * @return
     */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody UserAddRequest userAddRequest){
        ThrowUtils.throwIf(userAddRequest == null, ErrorCode.NOT_FOUND_ERROR);
        User user = new User();
        BeanUtils.copyProperties(userAddRequest, user);
        user.setUpdateTime(new Date());
        user.setCreateTime(new Date());
        user.setUserRole("user");
        log.info("注册用户信息{}",user);
        userService.save(user);
        return Result.ok(true);
    }

    /**
     * 分页查询
     * @param userQueryRequest
     * @return
     */
    @PostMapping("/page")
    @AuthCheck(mustRole = "admin")
    public Result<Page<User>> page(@RequestBody UserQueryRequest userQueryRequest){
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.NOT_FOUND_ERROR);
        Page<User> pageResult = userService.listPage(userQueryRequest);
        return Result.ok(pageResult);
    }
}
